package com.dianrui.fmi.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.Window;
import android.widget.ImageView;


import com.dianrui.fmi.Constant;
import com.dianrui.fmi.event.RfreshUnread;
import com.dianrui.fmi.util.GlideUtil;
import com.dianrui.fmi.util.MyUtil;
import com.dianrui.fmi.util.SPUtils;
import com.dianrui.fmi.util.StringUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.ButterKnife;

public abstract class BaseActivity extends AppCompatActivity {
    public SPUtils spUtils;
    private List<ImageView> imageViewList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//强制竖屏
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);//去除标题栏
        super.onCreate(savedInstanceState);
        setContentView(getContentViewId());
        spUtils = new SPUtils(this, Constant.cfgFileName, Context.MODE_PRIVATE);
        ButterKnife.bind(this);//以后没有必要每个界面写一遍butterknife
        initView();
        selectLanguage();//上次选择的语言
    }

    public void JumpActivitys(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    @Override
    protected void onStop() {//程序切后台
        super.onStop();
        if (MyUtil.isAppIsInBackground(BaseActivity.this)) {
            EventBus.getDefault().post(new RfreshUnread());
        }
    }


    public void selectLanguage() {
        //设置语言类型
        Resources resources = getResources();
        Configuration configuration = resources.getConfiguration();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        if (!StringUtils.isEmpty(spUtils.getString("language"))) {
            switch (spUtils.getString("language")) {
                case "english":
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                        configuration.setLocale(Locale.ENGLISH);//英语
                    }

                    break;
                case "burma":
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                        Locale myLocale = new Locale("my", "rMM");
                        configuration.setLocale(myLocale);//缅甸语
                    }
                    break;


                default:
                    break;
            }

            resources.updateConfiguration(configuration, displayMetrics);
        }

    }

    public void JumpLoginActivitys(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

    public  void readyGoThenKill(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
        finish();
    }

    /**
     * 保存图片引用
     *
     * @param imageView
     */
    public void addImageViewList(ImageView imageView) {
        if (imageViewList != null) {
            imageViewList.add(imageView);
        }
    }

    /**
     * 清除图片引用
     */
    public void clearImageViewList() {
        if (imageViewList != null) {
            for (int i = 0; i < imageViewList.size(); i++) {
                if (imageViewList.get(i) != null) {
                    GlideUtil.clearImg(imageViewList.get(i));
                    imageViewList.set(i, null);
                }
            }
            if (imageViewList != null) {
                imageViewList.clear();
            }
            imageViewList = null;
        }
    }


    public abstract int getContentViewId();

    public abstract void initView();

    @Override
    protected void onDestroy() {
        clearImageViewList();
        super.onDestroy();
    }

}
